package com.wjp.villagemp.controller.manage;

import com.wjp.villagemp.model.manage.StudentEntity;
import com.wjp.villagemp.model.sys.PageResult;
import com.wjp.villagemp.service.manage.StudentService;
import com.wjp.villagemp.utils.TimeHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生管理
 * @author lixr
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    private Logger log = LoggerFactory.getLogger(getClass());

    private final static Integer STUDENT_BIRTHDAY_LENGTH = 10;

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询所有学生信息
     * @param pageSize
     * @param page
     * @param studentName
     * @return
     */
    @GetMapping
    public PageResult studentList(int pageSize, int page, @RequestParam(required = false) String studentName) {
        PageResult pageResult = new PageResult();
        try {
            List<StudentEntity> listStudent = studentService.listStudent(pageSize, page * pageSize, studentName);
            pageResult.setData(listStudent);
            Integer student = studentService.countStudent(studentName);
            pageResult.setTotalCount(student);
        } catch (Exception e) {
            log.error("分页查询所有学生信息异常：" + e.getMessage());
        }
        return pageResult;
    }


    /**
     * 新增学生信息
     * @param studentEntity
     * @return
     */
    @PostMapping
    public Map<String, Object> insertStudent(@RequestBody StudentEntity studentEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (studentEntity != null) {
                // 将用户生日UTC时间格式转换为Date类型
                if (StringUtils.isNotBlank(studentEntity.getStudentBirthday())) {
                    Date birthDate = TimeHelper.getDate(studentEntity.getStudentBirthday());
                    studentEntity.setStudentBirthday(TimeHelper.formatDate(birthDate));
                }
                Integer key = studentService.insertStudent(studentEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("新增学生信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 更新学生信息
     * @param studentEntity
     * @return
     */
    @PutMapping
    public Map<String, Object> updateStudent(@RequestBody StudentEntity studentEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (studentEntity != null) {
                // 将用户生日UTC时间格式转换为Date类型
                if (StringUtils.isNotBlank(studentEntity.getStudentBirthday())) {
                    // Date类型不转换
                    if(studentEntity.getStudentBirthday().length() != STUDENT_BIRTHDAY_LENGTH){
                        Date birthDate = TimeHelper.getDate(studentEntity.getStudentBirthday());
                        studentEntity.setStudentBirthday(TimeHelper.formatDate(birthDate));
                    }
                }
                Integer key = studentService.updateStudent(studentEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("更新学生信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 批量删除学生信息
     * @param groupId
     * @return
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> removeStudent(@RequestBody List<Integer> groupId) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (groupId.size() >= 1) {
                boolean flag = false;
                for (Integer studentNum : groupId) {
                    Integer key = studentService.removeStudent(studentNum);
                    if (key > 0) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                }
                if (flag) {
                    resMap.put("msg", "success");
                }
            }
        } catch (Exception e) {
            log.error("批量删除学生信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 查询全部学生信息
     * @return
     */
    @GetMapping("/getAllStudents")
    public List<StudentEntity> getAllThemes() {
        List<StudentEntity> studentList = null;
        try {
            studentList = studentService.listAllStudent();
        } catch (Exception e) {
            log.error("查询全部学生信息异常：" + e.getMessage());
        }
        return studentList;
    }

    /**
     * 学号校验重复
     * @return
     */
    @GetMapping("/checkStudentNum")
    public Map<String, Object> checkStudentNum(String studentNum, @RequestParam(required = false) String type) {
        Map<String, Object> res = new HashMap<>(16);
        try {
            boolean hasRep = studentService.studentNumHasRep(studentNum);
            if (hasRep) {
                res.put("msg", 1);
            } else {
                res.put("msg", 0);
            }
        } catch (Exception e) {
            log.error("学号校验重复异常：" + e.getMessage());
        }
        return res;
    }
}
